//
//  XMUserDetailRemarkView.m
//  XunMi
//
//  Created by MacBook on 2024/2/26.
//  Copyright © 2024 TieZhu. All rights reserved.
//

#import "XMUserDetailRemarkView.h"

@interface XMUserDetailRemarkView () <UITextFieldDelegate, UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *bgV;

@property (weak, nonatomic) IBOutlet UILabel *titleLa;

@property (weak, nonatomic) IBOutlet UIView *fieldBg;

@property (weak, nonatomic) IBOutlet UITextField *textField;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bgVH;

@property (weak, nonatomic) IBOutlet UIView *textVBgV;

@property (weak, nonatomic) IBOutlet UITextView *textView;

@property (weak, nonatomic) IBOutlet UILabel *countLa;


@end


@implementation XMUserDetailRemarkView

- (void)awakeFromNib {
    [super awakeFromNib];
    kCornerRadius(_bgV, 20, 0, UIColor.clearColor);
    kCornerRadius(_fieldBg, 23, 0, UIColor.clearColor);
    kCornerRadius(_textVBgV, 10, 0, UIColor.clearColor);

    self.textField.delegate = self;
    [self.textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    
    self.textView.delegate = self;
}

- (void)setType:(XMEditUserInputType)type {
    _type = type;
    switch (type) {
        case XMEditUserInputTypeRemark:
            self.bgVH.constant = 198;
            self.fieldBg.hidden = NO;
            self.textVBgV.hidden = YES;
            self.titleLa.text = @"设置备注";
            self.textField.placeholder = @"请输入备注";
            break;
            
        case XMEditUserInputTypeNickname:
            self.bgVH.constant = 198;
            self.fieldBg.hidden = NO;
            self.textVBgV.hidden = YES;
            self.titleLa.text = @"修改昵称";
            self.textField.placeholder = @"请输入昵称";
            self.textField.text = [NSString xm_stringNoNullWith:kUserModel.nickname];
            break;
            
        case XMEditUserInputTypeSign:
            self.bgVH.constant = 269;
            self.fieldBg.hidden = YES;
            self.textVBgV.hidden = NO;
            self.titleLa.text = @"修改个性签名";
            self.textView.placeholder = @"请输入个性签名";
            self.textView.text = [NSString xm_stringNoNullWith:kUserModel.text_intro];
            self.countLa.text = kStringFormat(@"%ld/30", self.textView.text.length);
            break;
            
        default:
            break;
    }
}

- (IBAction)sureAct:(UIButton *)sender {
    [self endEditing:YES];
    if (self.sureBlock) {
        self.sureBlock(self.type == XMEditUserInputTypeSign ? self.textView.text : self.textField.text);
    }
}

- (IBAction)closeAct:(UIButton *)sender {
    [self endEditing:YES];
    if (self.closeBlock) {
        self.closeBlock();
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    return [textField resignFirstResponder];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self endEditing:YES];
}

- (void)textFieldDidChange:(UITextField *)textField {
    if (textField == self.textField) {
        if (textField.text.length > kUserNameMaxLength) {
            textField.text = [textField.text substringWithRange:NSMakeRange(0, kUserNameMaxLength)];
        }
    }
}

- (void)textViewDidChange:(UITextView *)textView {
    // 处理字符变化的逻辑
    if (textView.text.length > 30) {
        textView.text = [textView.text substringWithRange:NSMakeRange(0, 30)];
    }
    self.countLa.text = kStringFormat(@"%ld/30", textView.text.length);
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}

@end
